//
//  YSVipServiceDetailCtrl.m
//  Yueshi
//
//  Created by Northwan on 16/4/13.
//  Copyright © 2016年 悦食中国. All rights reserved.
//

#import "YSVipServiceDetailCtrl.h"
#import "YSCategoryDataTool.h"
#import "YSVipServiceItemCell.h"
#import "YSProductDetailCtrl.h"
#import "YSReviewsCtrl.h"

@interface YSVipServiceDetailCtrl ()<UITableViewDelegate,UITableViewDataSource,YSVipServiceItemCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *itemArray;
@property (assign, nonatomic) NSInteger advertId;
@end

@implementation YSVipServiceDetailCtrl

static NSString * const vipServiceDetailCell = @"YSVipServiceItemCell";

-(NSMutableArray *)itemArray{
    if (!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return  _itemArray;
}

-(instancetype)initWithAdvertId:(NSInteger)advertId{
    self = [super initWithNibName:@"YSVipServiceDetailCtrl" bundle:nil];
    if (self){
        self.advertId = advertId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData];
}

- (void)setupUI{
    [self setTitle:@"产品定制"];
    [self.tableView registerNib:[UINib nibWithNibName:vipServiceDetailCell bundle:nil] forCellReuseIdentifier:vipServiceDetailCell];
    self.tableView.rowHeight = 260;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [UIView new];
}

-(void)loadData{
    [YSCategoryDataTool loadVipServiceDetail:self.advertId success:^(YSVipServiceDetailRes *result) {
        if (result.isSucc){
            [self.itemArray removeAllObjects];
            [self.itemArray addObjectsFromArray:result.list];
            [self.tableView reloadData];
        }else{
            ShowTips(result.data)
        }
    } failure:^(NSError *error) {
         ShowTips(kRequestFailure)
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSVipServiceItemCell *cell = [tableView dequeueReusableCellWithIdentifier:vipServiceDetailCell forIndexPath:indexPath];
    cell.delegate = self;
    cell.selectionStyle  = UITableViewCellSelectionStyleNone;
    cell.model = self.itemArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)didSelectThumbViewWithProduct:(YSProductList *)product{
    YSProductDetailCtrl *ctrl =  [[YSProductDetailCtrl alloc]initWithProdId: product.prodId];
    [self.navigationController pushViewController:ctrl animated:YES];
}

-(void)didSelectThumbViewWithReview:(YSReviews *)review{
    YSReviewsCtrl *ctrl = [YSReviewsCtrl initWithReviews:review];
    [self.navigationController pushViewController:ctrl animated:YES];
}

@end
